'use strict';
import {
  TouchableWithoutFeedback
} from 'react-native'
import React from 'react'
import {UI} from '@hyext/hy-ui'
import url from './mystr'
import './request.hycss'

const { Text,
  View,
  ScrollView,
  Button,
  Image
} = UI;


const setResult_ = (num)=>{
  console.log("setResult_")
  
    let args = [];
    args[0] = {};
    args[0].header = { "x-header": "foo" };
    args[0].url = url+'setResult/'+num;
    console.log(args[0].url)
    // args[0].url = 'http://182.92.118.106:8080/setResult/3';
    args[0].method = "POST";
    args[0].data = { };
    // args[0].dataType = "json";
    hyExt
      .request(args[0])
      .then((resp) => {
        // var k_result = JSON.stringify(resp.data.result) //***这是关键
        var k_result = JSON.stringify(resp.data) //***这是关键
        //{"result":["葫芦","虾米","铜钱"]}
        //k_result = ["葫芦","虾米","铜钱"]
        console.log(k_result+"kresult")

        hyExt.storage.setItem('counted_result', k_result).then(() => {
        }).catch(err => {
        })
      })
      .catch((err) => {
        console.log("setResult_ fail")
        console.log(err)
      });
}

const changeToIndex_ = (result)=>{
    if(result == '螃蟹'||result == 'PX'){
      return 0
    }else if(result == '鱼儿'||result == 'YE'){
      return 1
    }else if(result == '虾米' || result == 'XM'){
      return 2
    }else if(result == '铜钱' || result == 'TQ'){
      return 3
    }else if(result == '公鸡' || result == 'GJ'){
      return 4
    }else if(result == '葫芦' || result == 'HL'){
      return 5
    }
  
}


const getOrders_ = (num, app)=>{
  console.log("getOrders_")
    let args = [];
    args[0] = {};
    args[0].header = { "x-header": "foo" };
    args[0].url = url+'getOrders/'+num;
    args[0].method = "POST";
    args[0].data = {};
    args[0].dataType = "json";
    hyExt
      .request(args[0])
      .then((resp) => {
        // var k_result = JSON.stringify(resp.data.result) //***这是关键
        console.log("成功获取请求数据："+JSON.stringify(resp))
        // let resp_obj = JSON.parse(resp)
        let array = resp.data.users
        console.log("用户们："+array)
        const show_viewp = array.map((item, index)=>{
          return(
            <View className = "user_item">
              <Text >昵称：{item.userNick}</Text>
              <Text >得分：{item.grade}</Text>
            </View>
            
          )
        })
        app.setState({show_view: show_viewp, msg: '666'})
        return resp
      })
      .catch((err) => {
        console.log("getOrders_ fail")
        console.log(err)
        return 'false'
      });
}

const sendYz_ = (myinfo)=>{
  console.log("sendYz_")
    let args = [];
    args[0] = {};
    args[0].header = { "x-header": "foo" };
    args[0].url = url+'sendYz?json='+myinfo;
    console.log(args[0].url)
    args[0].method = "POST";
    // args[0].data = {json: myinfo};
    args[0].data = {};
    args[0].dataType = "json";
    hyExt
      .request(args[0])
      .then((resp) => {
        // var k_result = JSON.stringify(resp.data.result) //***这是关键
        console.log("sendYz的response:"+resp)
      })
      .catch((err) => {
        console.log("sendYz_ fail  很正常")
        console.log(err)
      });
}

const getUserResult_ = (id, app)=>{
  console.log("进入getUserResult_")
    let args = [];
    args[0] = {};
    args[0].header = { "x-header": "foo" };
    args[0].url = url+'getUserResult?userid='+id;
    args[0].method = "POST";
    args[0].data = {};
    args[0].dataType = "json";
    console.log("用户的uid为："+id)
    hyExt
      .request(args[0])
      .then((resp) => {
        var k_result = JSON.stringify(resp) //***这是关键
        console.log("kkkkkkkresult（getUserResult）"+k_result)
        console.log("我的得分为："+resp.data)
        app.setState({my_result: resp.data})
      })
      .catch((err) => {
        console.log("getUserResult_ fail")
        console.log(err)
      });
}


const changeName_ = (list)=>{
  for(var i = 0; i < list.length; i++){
    switch(list[i].name){
      case 'PX': list[i].name = '螃蟹';break;
      case 'YE': list[i].name = '鱼儿';break;
      case 'XM': list[i].name = '虾米';break;
      case 'GJ': list[i].name = '公鸡';break;
      case 'TQ': list[i].name = '铜钱';break;
      case 'HL': list[i].name = '葫芦';break;
    }
  }
}

export var setResult =  setResult_
export var getOrders = getOrders_
export var sendYz = sendYz_
export var getUserResult = getUserResult_
export var changeName = changeName_
export var changeToIndex = changeToIndex_